/**
 * @file qq_validation_msg.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QQ != 0
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qq_validation_msg_list_btn_event_cb(lv_obj_t * btn, lv_event_t event);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void qq_create_validation_msg_list(watch_qq_undecided_msg_successread_t * msg)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * cont = lv_obj_get_child(ext->friend, NULL);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_bg_color(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_clean(cont);
    lv_obj_set_hidden(ext->leds_content, true);

    lv_obj_t * back_img = qq_create_back_img(cont, QQ_VALID_MSG);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_VALIDATION_MSG);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_align(label, back_img, LV_ALIGN_OUT_RIGHT_MID, 0, 0);
    lv_obj_t * list = qq_create_list(cont);
    lv_obj_set_pos(list, QQ_PAD_HOR, lv_obj_get_height(back_img) + QQ_PAD_VER);
    lv_obj_set_size(list, LV_HOR_RES - QQ_PAD_HOR * 2, LV_VER_RES - lv_obj_get_height(back_img) - QQ_PAD_VER * 2);

    lv_obj_t * btn;
    qq_validmsg_btn_ext_t * btn_ext = NULL;
    watch_qq_validation_msg_t * val_msg = _lv_ll_get_head(&msg->validation_msgs_ll);
    while(val_msg) {
        btn = qq_list_add(list, ICON_QQ_VALID_MSG, NULL, (void *)val_msg, LIST_TYPE_VALIDATION_MSG);
        btn_ext = (qq_validmsg_btn_ext_t *)lv_obj_allocate_ext_attr(btn, sizeof(qq_validmsg_btn_ext_t));
        btn_ext->reqtype = val_msg->reqtype;
        btn_ext->main_sourceid = val_msg->main_sourceid;
        memcpy(btn_ext->openid, val_msg->friend_openid, 64);
        lv_obj_set_event_cb(btn, qq_validation_msg_list_btn_event_cb);
        printf("%s: reqtype = %d\n", __FUNCTION__, val_msg->reqtype);
        if(val_msg->reqtype == WATCH_QQ_UNDECIDED_REQTYPE_REQ_ADD_FRIEND) {
            lv_obj_t * newmsg_tip = lv_img_create(btn, NULL);
            lv_img_set_src(newmsg_tip, ICON_QQ_UNREAD_MSG);
            lv_obj_align(newmsg_tip, btn, LV_ALIGN_IN_RIGHT_MID, -lv_obj_get_width(newmsg_tip), 0);
        }
        val_msg = _lv_ll_get_next(&msg->validation_msgs_ll, val_msg);
    }
    if(msg->over == 1) {
        lv_obj_t * nodata_label = lv_label_create(list, NULL);
        lv_label_set_text_id(nodata_label, WATCH_TEXT_ID_QQ_NO_MORE_DATA);
        lv_obj_set_style_local_text_color(nodata_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
        lv_obj_set_style_local_text_font(nodata_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_16);
        lv_obj_align(nodata_label, lv_page_get_scrollable(list), LV_ALIGN_IN_BOTTOM_MID, 0, 0);
        lv_page_glue_obj(nodata_label, true);
    } else {
    }
}

static void qq_validation_msg_list_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_qq_obj_ext_t * ext = qq_get_ext();
    ext->friend_list_act_btn = btn;
    qq_validmsg_btn_ext_t * btn_ext = lv_obj_get_ext_attr(btn);
    lv_obj_t * cont = qq_create_bg_cont(ext->friend, LV_OPA_90, WATCH_TEXT_ID_QQ_PULLING_DATA);
    lv_watch_set_free_ptr(cont, (void *)btn_ext);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_validation_msg_detail_timer_cb, NULL, false);
#else
    watch_qq_get_friend_info_req_t * get_friendreq = (watch_qq_get_friend_info_req_t *)lv_mem_alloc(sizeof(watch_qq_get_friend_info_req_t));
    memcpy(get_friendreq->openid, btn_ext->openid, 64);
    qq_set_get_friend_info_type(QQ_GET_VALIDMSG_INFO);
    watch_qq_get_friend_info_req(get_friendreq);
#endif
}

void qq_ack_friend_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_obj_t * cont = lv_obj_get_parent(btn);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_get_validmsg_timer_cb, NULL, false);
#else
    qq_validmsg_btn_ext_t * btn_ext = (qq_validmsg_btn_ext_t *)lv_watch_get_free_ptr(cont);
    uint16_t txtId = lv_watch_get_free_num(lv_obj_get_child(btn, NULL));
    watch_qq_ack_add_friend_req_t * req = (watch_qq_ack_add_friend_req_t *)lv_mem_alloc(sizeof(watch_qq_ack_add_friend_req_t));
    if(txtId == WATCH_TEXT_ID_QQ_AGRRE)
        req->ack_type = WATCH_QQ_ACK_TYPE_AGREE;
    else
        req->ack_type = WATCH_QQ_ACK_TYPE_REJECT;
    memcpy(req->to_openid, btn_ext->openid, 64);
    req->rejectmsg = NULL;
    req->main_sourceid = btn_ext->main_sourceid;
    req->sub_sourceid = WATCH_QQ_SOURCE_FROM_QQ_ID;
    req->remark = NULL;
    watch_qq_ack_add_friend_req(req);
#endif
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_del(cont);
    lv_obj_del(lv_obj_get_child(ext->friend, NULL));
    qq_create_bg_cont(ext->friend, LV_OPA_90, WATCH_TEXT_ID_QQ_PULLING_DATA);
}

uint16_t qq_get_txtId_from_reqtype(uint8_t reqtype)
{
    uint16_t txtId = 0;
    if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_WAITING_FRIEND_VERIFY)
        txtId = WATCH_TEXT_ID_QQ_VALIDATION_MSG_SENT;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_REQ_ADD_FRIEND)
        txtId = WATCH_TEXT_ID_QQ_REQUEST_ADD_FRIEND;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_AFREE)
        txtId = WATCH_TEXT_ID_QQ_AGREED;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_AFREE_AND_ADD)
        txtId = WATCH_TEXT_ID_QQ_AGREED_ADDED;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_AFREE_AND_ADD)
        txtId = WATCH_TEXT_ID_QQ_FRIEND_AGREED_ADDED;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_AFREE)
        txtId = WATCH_TEXT_ID_QQ_FRIEND_AGREED;
    else if(reqtype == WATCH_QQ_UNDECIDED_REQTYPE_REFUSE || reqtype == WATCH_QQ_UNDECIDED_REQTYPE_FRIEND_REFUSE)
        txtId = WATCH_TEXT_ID_QQ_REFUSED;

    return txtId;
}

void qq_validation_msg_detail_show(watch_qq_user_info_t * info)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();

    lv_obj_t * cont = lv_obj_get_child(ext->friend, NULL);
    qq_validmsg_btn_ext_t * btn_ext = (qq_validmsg_btn_ext_t *)lv_watch_get_free_ptr(cont);
    uint8_t reqtype = btn_ext->reqtype;
    strncpy(btn_ext->qqid, info->qqid, QQ_ID_LEN);
    printf("%s: reqtype = %d\n", __FUNCTION__, reqtype);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_bg_color(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_clean(cont);

    lv_obj_t * back_img = qq_create_back_img(cont, QQ_VALID_MSG_DETAIL);

    lv_obj_t * head_img = lv_img_create(cont, NULL);
    //req head img
    char * path = watch_qq_get_friend_head_path(info->openid);
    if(path != NULL) {
        lv_img_set_src(head_img, path);
        lv_mem_free(path);
    } else {
        lv_img_set_src(head_img, ICON_QQ_DEF_HEAD);
        watch_qq_get_data_req_t * get_data_req = lv_mem_alloc(sizeof(watch_qq_get_data_req_t));
        get_data_req->url = lv_strdup(info->headurl);
        qq_add_get_data_info(QQ_DATA_VALID_FRIEND_HEAD, 0, get_data_req->url);
        watch_qq_get_data_req(get_data_req);
    }
    lv_obj_align(head_img, NULL, LV_ALIGN_IN_TOP_MID, 0,  lv_obj_get_height(back_img) + QQ_PAD_VER * 2);

    lv_obj_t * name_label = lv_label_create(cont, NULL);
    lv_label_set_text(name_label, info->nickname);
    lv_obj_set_style_local_text_color(name_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_label_set_long_mode(name_label, LV_LABEL_LONG_DOT);
    lv_obj_set_width(name_label, LV_HOR_RES * 2 / 3);
    lv_label_set_align(name_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(name_label, head_img, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 4);

    uint16_t txtId = qq_get_txtId_from_reqtype(reqtype);
    lv_obj_t * req_label = lv_label_create(cont, NULL);
    lv_label_set_text_id(req_label, txtId);
    lv_obj_set_style_local_text_color(req_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_align(req_label, name_label, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 5);

    if(reqtype == 1) {
        lv_obj_t * refuse_btn = lv_btn_create(cont, NULL);
        lv_obj_set_size(refuse_btn, LV_HOR_RES / 2 - QQ_PAD_HOR, LV_VER_RES / 5 - QQ_PAD_VER);
        lv_obj_set_style_local_bg_color(refuse_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_RED_NORMAL);
        lv_obj_set_style_local_bg_color(refuse_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_RED_PRESSED);
        lv_obj_set_style_local_radius(refuse_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 50);
        lv_obj_set_event_cb(refuse_btn, qq_ack_friend_btn_event_cb);
        lv_obj_t * refuse_label = lv_label_create(refuse_btn, NULL);
        lv_label_set_text_id(refuse_label, WATCH_TEXT_ID_QQ_REFUSE);
        lv_obj_set_style_local_text_color(refuse_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_align(refuse_btn, req_label, LV_ALIGN_OUT_BOTTOM_MID, -lv_obj_get_width(refuse_btn) / 2, lv_obj_get_height(head_img) / 3);

        lv_obj_t * agree_btn = lv_btn_create(cont, refuse_btn);
        lv_obj_set_style_local_bg_color(agree_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_set_style_local_bg_color(agree_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
        lv_obj_t * agree_label = lv_label_create(agree_btn, NULL);
        lv_label_set_text_id(agree_label, WATCH_TEXT_ID_QQ_AGRRE);
        lv_obj_set_style_local_text_color(agree_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
        lv_obj_align(agree_btn, req_label, LV_ALIGN_OUT_BOTTOM_MID, lv_obj_get_width(agree_btn) / 2, lv_obj_get_height(head_img) / 3);
    }
}

void qq_validation_msg_detail_update_headimg(void * src)
{
    if(!strncmp(src, ICON_QQ_DEF_HEAD, strlen(ICON_QQ_DEF_HEAD)))
        return;
    lv_qq_obj_ext_t * ext = qq_get_ext();
    uint8_t child_cnt = lv_obj_count_children(ext->friend);
    printf("%s: child cnt = %d!\n", __FUNCTION__, child_cnt);
    if(child_cnt < 3) return;
    else {
        lv_obj_t * cont = lv_obj_get_child(ext->friend, NULL);
        lv_obj_t * head_img = lv_obj_get_child_back(cont, lv_obj_get_child_back(cont, NULL));
        lv_img_set_src(head_img, src);
    }
    qq_validmsg_btn_ext_t * btn_ext = lv_obj_get_ext_attr(ext->friend_list_act_btn);
    watch_qq_friend_info_t info;
    memset(&info, 0, sizeof(watch_qq_friend_info_t));
    strncpy(info.openid, btn_ext->openid, 64);
    strncpy(info.qqid, btn_ext->qqid, QQ_ID_LEN);
    strncpy(info.headpath, (char *)src, QQ_HEAD_PATH_LEN);
    watch_qq_add_friend(&info);
}

void qq_validation_new_msg_tip(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * list = lv_obj_get_child_back(ext->friend, NULL);
    lv_obj_t * btn = lv_list_get_next_btn(list, NULL);
    if(lv_obj_count_children(btn) == 3) return;
    lv_obj_t * newmsg_tip = lv_img_create(btn, NULL);
    lv_img_set_src(newmsg_tip, ICON_QQ_UNREAD_MSG);
    lv_obj_align(newmsg_tip, btn, LV_ALIGN_IN_RIGHT_MID, -lv_obj_get_width(newmsg_tip), 0);
}

#endif /*USE_LV_WATCH_QQ*/
